# import numpy as np
import cv2
import dlib
import dlib_angle

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')


def detect_(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    img_gray = cv2.equalizeHist(img_gray)

    rects = detector(img_gray, 0)
    landmarks = []
    for rc in rects:
        landmarks.append(predictor(img, rc).parts())

    angles = dlib_angle.angle(img, rects, landmarks)

    if len(rects) > 0:
        return {
            'detectFace': int(1),
            'x': int(angles[0]['x']),
            'y': int(angles[0]['y']),
            'z': int(angles[0]['z']),
        }
    else:
        return {
            'detectFace': int(0)
        }
